import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:h_flutter_119/generated/assets.gen.dart';
import 'package:h_flutter_119/page/getx/ThrottleTest.dart';
import 'package:h_flutter_119/page/getx/ValueBuilderView.dart';
import 'package:h_flutter_119/page/getx/test/ChangeLoadStateLayout.dart';
import 'package:h_flutter_119/page/getx/test/eventbus/test_event_bus_page.dart';
import 'package:h_flutter_119/page/getx/test/interceptor/page/LoginInterceptorPage.dart';
import 'package:h_flutter_119/page/getx/test/singleton/singleton.dart';
import 'package:h_flutter_119/page/home/widget/base/LoadStateLayout.dart';

import '../../api/Api.dart';
import '../../http/httpUtils.dart';
import '../../utils/event_bus.dart';
import '../../utils/log_utils.dart';
import '../home/model/home_eye_entity.dart';
import '../home/model/home_eye_itemlist_entity.dart';
import '../home/widget/ItemHomeWidget.dart';

class GetXPage extends GetView<Mine2PageController> {
  // Rx<LoadState> state = LoadState.State_Loading.obs;
  LoadState state = LoadState.State_Loading;
  RxString btText = "change2ErrorStatus".obs;

  // Future<void> getResult() async{
  //   //等待所有数据请求完成
  //   List<dynamic> result = await Future.wait([HttpUtils.getInstance().asyncRequest('path'),HttpUtils.getInstance().asyncRequest("path")]);
  //   for (var value in result) {
  //     if(value is String) {
  //       ...
  //     }else if(value is Map) {
  //       ...
  //     }
  //   }
  // }

  void updateLoadState(LoadState state) {
    // this.state.value = state;
    this.state = state;
    controller.update();
  }

  void registerEventBus() {
    bus.on('test', (arg) {
      LogD('GetPage receiveMsg=>$arg');
      Fluttertoast.showToast(msg: arg.toString());
    });
  }

  @override
  Widget build(BuildContext context) {
    registerEventBus();
    return controller.obx((state) => Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            // ListView.builder(itemBuilder: (context, index) {
            //   return ItemHomeWidget(SwiperController(), controller.items[index]);
            // },itemCount: controller.items.length,),
            // ChangeLoadStateLayout(),
            ElevatedButton(
                onPressed: () {
                  // btText.value = btText.value+this.state.value.name;
                  Navigator.push(context, MaterialPageRoute(builder: (context) => LoginInterceptorPage()));
                },
                child: Text('Go LoginInterceptorPage')),
            TestEventBusPage(),
            Column(
              children: [
                ElevatedButton(
                    onPressed: () {
                      // btText.value = btText.value+this.state.value.name;
                      updateLoadState(LoadState.State_Error);
                    },
                    child: Text(btText.value)),
                ElevatedButton(
                    onPressed: () {
                      updateLoadState(LoadState.State_Loading);
                    },
                    child: Text('change2Loading')),
                ElevatedButton(
                    onPressed: () {
                      updateLoadState(LoadState.State_Empty);
                    },
                    child: Text('change2Empty')),
                ElevatedButton(
                    onPressed: () {
                      updateLoadState(LoadState.State_Success);
                    },
                    child: Text('change2Success')),
              ],
            ),
            Container(
              height: 300,
              color: Colors.blueAccent,
              child: LoadStateLayout(
                successWidget: Text(
                  'i am success Layout',
                  style: TextStyle(
                    color: Colors.deepOrange,
                    fontSize: 15,
                  ),
                ),
                state: this.state,
                errorRetry: () async {
                  Fluttertoast.showToast(msg: 'retry');
                  updateLoadState(LoadState.State_Loading);
                  await Future.delayed(
                    Duration(seconds: 2),
                    () {
                      updateLoadState(LoadState.State_Success);
                    },
                  );
                },
              ),
            ),
          ],
        ));
  }


}

class Mine2PageController extends SuperController {
  RxList<EyeItemList> items = <EyeItemList>[].obs;
  int _page = 1;
  String date = "";

  @override
  void onReady() {
    LogD('>>>>>>>onReady');
    getHomeData();
  }

  void getHomeData() async {
    if (_page == 1) {
      items.clear();
      date = "";
    }
    var response = await HttpUtils.getInstance()
        .get(Api.HOME_DATA, data: {"date": date, "num": _page});
    var homeDataMap = json.decode(response.toString());
    var entity = HomeEyeEntity.fromJson(homeDataMap);
    var nextPage = entity.nextPageUrl ?? "";
    var split = nextPage.split("?");
    var split2 = split[1];
    var split3 = split2.split("&");
    var split4 = split3[0].split("=");
    date = split4[1];
    LogE(
        "split=$split split2=$split2 split3=$split3 split4=$split4 date=$date");
    items.addAll(entity.issueList?.first.itemList ?? []);
    change(null, status: RxStatus.success());
  }

  @override
  void onDetached() {
    LogD('>>>>>>>onDetached');
  }

  @override
  void onInactive() {
    LogD('>>>>>>>onInactive');
  }

  @override
  void onPaused() {
    LogD('>>>>>>>onPaused');
  }

  @override
  void onResumed() {
    LogD('>>>>>>>onResumed');
  }
}
